package train;
import java.util.*;
import traveler.Traveler;

/**
 * Train holds lists of Travelers which have destinations.
 * 
 * @author Team Switchboard Boxcar
 */
public class Train {
	
	private int DepartureTime;
	private int TotalPass;
	private ArrayList<Traveler> Passengers[];
	private int MaxPassengers;
	private int Direction;
	
	/**
	 * 
	 * This Event Constructor is the Basic Constructor for class Event. It takes
	 * in no inputs setting both the time and ID to imaginary values.
	 * 
	 * @see OccurenceTime
	 * @see EventID
	 */
	public Train() 
	{
		DepartureTime = -1;
		TotalPass = 0;
		MaxPassengers = 100;
		Direction = 1;	
		Passengers = new ArrayList[4];
		for (int i = 0; i < Passengers.length; i++ )
			Passengers[i] = new ArrayList<Traveler>();
	}

	/**
	 * This Event Constructor takes inputs for the Train class.
	 * 
	 * @param depart
	 *            Time the train last departed
	 * @param maxPass
	 *            Maximum Travelers allowed on each train
	 * @param direction
	 *            Direction the train is traveling (negative = CCLKWS, positive =
	 *            CLKWS)
	 */
	public Train(int depart, int maxPass, int direction) 
	{
		DepartureTime = depart;
		TotalPass = 0;
		MaxPassengers = maxPass;
		Direction = direction;
		Passengers = new ArrayList[4];
		for (int i = 0; i < Passengers.length; i++ )
			Passengers[i] = new ArrayList<Traveler>();
	}
	
	
	/**
	 * isTrainFull is used to determine whether addtional travelers can be added
	 * to this train.
	 * 
	 * @return isFull Boolean value which is true if the train is full
	 */
	public boolean isTrainFull() 
	{	return TotalPass >= MaxPassengers; 	}

	
	/**
	 * AddPassenger is the function that can be called to add addtiional
	 * travelers to the train. Total Passengers on the train are updated when
	 * this function is called.
	 * 
	 * @param addTraveler
	 *            The Traveler boarding the train
	 */
	public void AddPassenger(Traveler addTraveler) 
	{	
		Passengers[addTraveler.GetDestination()].add(addTraveler);
		TotalPass++;
	}

	/**
	 * UnloadPassengers is used to unload passengers who have arrived at their
	 * destination. Passengers continuing on to other destinations will not be
	 * unlaoded using this function. This function will update the total number
	 * of travelers on the train.
	 * 
	 * @param currentStop
	 *            The destination which represents the current destination (0 -
	 *            4)
	 * 
	 * @return An array of travelers who are returning to the station
	 */
	public List<Traveler> UnloadPassengers(int currentStop) 
	{
		List<Traveler> travelers = Passengers[currentStop];
		TotalPass = TotalPass - Passengers[currentStop].size();
		Passengers[currentStop] = new ArrayList<Traveler>();
		return travelers;
	}


	/**
	 * GetEffect returns an EventID for the specific event. This can be used to
	 * distinguish between the 5 seperate event types.
	 * 
	 * @param depart
	 *            Time the train last departed
	 */
	public void SetDepartTime(int depart) 
	{	DepartureTime = depart;	}

	
	
	/**
	 * ReturnDirection returns a value representing the direction of the train.
	 * Negative numbers reflect a counterclockwise direction, positive numbers
	 * reflect a clockwise direction.
	 * 
	 * @return Returns an integer representing the direction (CLKWS / CCLKWS) of
	 *         the train
	 */
	public int returnDirection()
	{	return Direction;	}
	
	
	
	/**
	 * returnDepartTime returns a value representing the time the train departed
	 * the station.
	 * 
	 * @return Returns an integer representing the departure time of the train.
	 */
	public int returnDepartTime()
	{	return DepartureTime;	}
	
	/**
	 * IsTravelingCW returns the direction of the train.
	 * 	 * 
	 * @return Returns true if train is traveling Clockwise and false if it is
	 * 			traveling Counter-Clockwise.
	 */
	public boolean IsTravelingCW()
	{	return Direction > 0;	}
}